/*
 * Created by JFormDesigner on Mon Jun 07 09:14:42 CST 2021
 */

package com.J1ay.carsys.gui.User;

import java.awt.event.*;

import com.J1ay.carsys.gui.User.Dialog.HuanCarDialog;
import com.J1ay.carsys.gui.Welcome;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.BowCar;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Car;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.Impl.UserServiceImpl;
import com.J1ay.carsys.service.UserService;

import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class HuanCar extends JFrame {
    Bower nowbower;
    BowCar[] bowCars;
    BowCar huan;
    UserService userService = new UserServiceImpl();
    public HuanCar(Bower bower) {
        initComponents();
        nowbower = bower;
    }
    public void getCarByUId(){
        bowCars = (BowCar[]) userService.getCarByUId(nowbower.getUId()).getData();
        String[] data = new String[bowCars.length];
        for(int i=0;i< bowCars.length;++i){
            data[i] = bowCars[i].toString();
        }
        this.list1.setListData(data);

    }
    public void start() {
        this.setVisible(true);
        this.getCarByUId();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void backActionPerformed(ActionEvent e) {
        // TODO add your code here
        new UserMain(nowbower).start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }

    private void exitActionPerformed(ActionEvent e) {
        // TODO add your code here
        new Welcome().start();
        // 关闭窗口
        JButton jButton = (JButton) e.getSource();
        Container container = jButton.getParent();
        while (!(container instanceof JFrame)) {
            container = container.getParent();
        }
        ((JFrame)container).dispose();
    }

    private void huanCarActionPerformed(ActionEvent e) {
        // TODO add your code here
        int index = this.list1.getSelectedIndex();
        if(index == -1){
            new Warning(this,"选中记录为空哦");
        }
        else {
            BowCar record = bowCars[index];
            new HuanCarDialog(this,record,nowbower).start();
            System.out.println(record);
        }
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        scrollPane1 = new JScrollPane();
        list1 = new JList();
        back = new JButton();
        label1 = new JLabel();
        exit = new JButton();
        huanCar = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7ba1\u7406\u7cfb\u7edf");
        Container contentPane = getContentPane();

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(list1);
        }

        //---- back ----
        back.setText("\u8fd4\u56de");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                backActionPerformed(e);
            }
        });

        //---- label1 ----
        label1.setText("\u6240\u5904\u4f4d\u7f6e\uff1a\u8fd8\u8f66");

        //---- exit ----
        exit.setText("\u9000\u51fa");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exitActionPerformed(e);
            }
        });

        //---- huanCar ----
        huanCar.setText("\u8fd8\u8f66");
        huanCar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                huanCarActionPerformed(e);
            }
        });

        //---- label2 ----
        label2.setText("\u6c7d\u8f66\u7f16\u53f7");

        //---- label3 ----
        label3.setText("\u7528\u6237\u8d26\u53f7");

        //---- label5 ----
        label5.setText("\u7ecf\u529e\u4eba\u5de5\u53f7");

        //---- label6 ----
        label6.setText("\u501f\u8f66\u5929\u6570");

        //---- label7 ----
        label7.setText("\u5355\u4ef7/\u5929");

        //---- label8 ----
        label8.setText("\u501f\u8f66\u65f6\u95f4");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 539, GroupLayout.PREFERRED_SIZE)
                            .addGap(12, 12, 12)
                            .addComponent(huanCar, GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                            .addGap(12, 12, 12))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addComponent(back)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 336, Short.MAX_VALUE)
                            .addComponent(exit))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(42, 42, 42)
                            .addComponent(label3, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
                            .addGap(40, 40, 40)
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label5, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(label8, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 86, Short.MAX_VALUE)))
                    .addGap(20, 20, 20))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(back)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label1)
                            .addComponent(exit)))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label5, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label6, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label7, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
                        .addComponent(label8, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE)
                        .addComponent(huanCar, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(32, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JScrollPane scrollPane1;
    private JList list1;
    private JButton back;
    private JLabel label1;
    private JButton exit;
    private JButton huanCar;
    private JLabel label2;
    private JLabel label3;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
